/**
 * Created by qiang.qian on 2016/12/2.
 */

function loading() {
    $.LoadingOverlay("show", {zIndex: 6});
}

function loaded() {
    $.LoadingOverlay("hide", {zIndex: 6});
}

function post(url, params, callback, callbackArgs) {
    loading();
    $.ajax({
        type: "POST",
        url: "/cm/" + url,
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(params),

        complete: function () {
            loaded();
        },

        success: function (resp) {
            console.log(resp);
            if (resp.result == 0) {
                callback(resp, callbackArgs);
            } else {
                if (resp.message && resp.message != '') {
                    error(resp.message);
                } else {
                    error("服务器错误，请重试！");
                }
            }
        }
    });
}

function postSync(url, params, callback, callbackArgs) {
    $.ajax({
        type: "POST",
        url: "/cm/" + url,
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(params),
        beforeSend: function (xhr) {
            xhr.setRequestHeader(header, token);
        },

        success: function (resp) {
            console.log(resp);
            if (resp.result == 0) {
                callback(resp, callbackArgs);
            } else {
                if (resp.message && resp.message != '') {
                    error(resp.message);
                } else {
                    error("数据保存失败，请重试！");
                }
            }
        }
    });
}

function getSync(url, params, callback, callbackArgs) {
    $.ajax({
        type: "GET",
        url: "/cm/" + url,
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(params),
        beforeSend: function (xhr) {
            xhr.setRequestHeader(header, token);
        },

        success: function (resp) {
            console.log(resp);
            if (resp.result == 0) {
                callback(resp, callbackArgs);
            } else {
                if (resp.message && resp.message != '') {
                    error(resp.message);
                } else {
                    error("数据保存失败，请重试！");
                }
            }
        }
    });
}
